$(function(){
	initPage();//已完结 尚未归档的事件源信息
	initArchivedReportPage();//已归档事件源的归档信息
});
//=====================事件源关联的内部事件========================
var serverData3= {};
var serverTable3;
function showDetailRegisters(data){
	
//	var reportId=data.report.id;
	var reportId=data.reportId;
	serverData3.key=reportId;
	serverTable3 = $('#dataTable3').DataTable({
		"serverSide" : true,
		"ordering" : false,
		"ajax" : {
			"dataType" : "json",
			"type" : "post",
			"cache" : false,
			"url" : window.rootPath
					+ '/ui/archiveReport/showDetailRegisters',
			"data" : function(d) {
				return $.extend({}, d,serverData3);
			}
		},
		columns : [
				{
					data : null,
					defaultContent : ""
				},
				{
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
                    		var operate ='<button type="button" class="btn btn-sm btn-info detailBtn" title="详情"><i class="fa fa-edit"></i></button>';
                        return operate;
                    }
                },
				{
					data : 'registerEventName'
				},{
					data : 'registerEventContent'
				},{
					data : 'registerEventMemo'
				}, {
					data : 'registerEventIdentify',
				}, {
					data : 'registerEventType.eventName'
				}, {
					data : 'registerEventEvaluateLevel.gradingName'
				}, {
					data : 'registerEventSeriousLevel.title'
				}, {
					data : 'registerEventReportTime',
					render:function(data,type,full,meta){
						if(data){
							return moment(data).format("YYYY/MM/DD HH:mm:ss");
						}else{
							return '';
						}
					}
				}, {
					data : 'registerEventToNetGrid.gridName'
				}, {
					data : 'registerEventState.title'
				}, {
					data : 'registerEventCreateUser.name'
				}
		],
		"drawCallback" : function(oSettings) {
			var k = 0;
			var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
			for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
				$('td:eq(0)',oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
				k++;
			}
			serverTable3.off('click', 'tr');
			serverTable3.on('click', 'tr', function (event) {
                 event = event || window.event;
                 var target = event.target || event.scrElement;
                 var data = serverTable3.row(this).data();
                 if (target.className.indexOf('fa-edit') != -1 ||target.className.indexOf('detailBtn') != -1) {
                     showDetail(data);
                 }
                 return;
             });
		}
	});
	serverTable3.draw();
	$("#details").show();
	
    var showDetail=function(o){
    		if(!o) return;
    		window.location = window.rootPath
    		+ '/ui/eventRegister?eventReportIdentify='+o.registerEventToReportEvent.id+"&eventRegisterIdentify="+o.id;
    }
};
//===============================撤销与查询===========================
$("#selectT2").click(function(){
	serverData2.key=$("#search2").val();
	serverTable2.draw();
});
$("#selectT1").click(function(){
	serverData.key=$("#search1").val();
	serverTable.draw();
});
$("#resetT1").click(function(){
	$("#search1").val("");
	serverData.key='';
	serverTable.draw();
});
$("#resetT2").click(function(){
	$("#search2").val("");
	serverData2.key='';
	serverTable2.draw();
});
//===================================已归档事件源的归档信息===================================
var serverData2 = {};
var serverTable2;
function initArchivedReportPage(){
	serverTable2 = $('#dataTable2').DataTable({
		"serverSide" : true,
		"ordering" : false,
		"ajax" : {
			"dataType" : "json",
			"type" : "post",
			"cache" : false,
			"url" : window.rootPath
					+ '/ui/archiveReport/initArchivedPage',
			"data" : function(d) {
				return $.extend({}, d,serverData2);
			}
		},
		columns : [
				{
					data : null,
					defaultContent : ""
				},{
					data : 'reportMemo',
					render : function(data, type, full, meta) {
						if(!data){
							data='无备注消息';
						}
						
						return data;
					}
				},{
					data : 'memo',
					render : function(data, type, full, meta) {
						if(!data){
							data='归档时未提交备注信息';
						}
						return data;
					}
				},{
					data : 'content',
					render : function(data, type, full, meta) {
						if(!data){
							data='归档时未提交内容信息';
						}
						return data;
					}
				}, {
					data : 'archiveUserName',
				}, {
					data : 'archeveDate',
					 render: function (data, type, full, meta) {
                         if (data)
                             return moment(data).format('YYYY/MM/DD HH:mm:ss');
                         else
                             return '';
                  }
				}
		],
		"drawCallback" : function(oSettings) {
			var k = 0;
			var total = oSettings.aiDisplay.length
					+ oSettings._iDisplayStart;
			for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
				$('td:eq(0)',oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
				k++;
			}
			serverTable2.off('click', 'tr');
			serverTable2.on('click','tr',function(event) {
				event = event || window.event;
				var target = event.target|| event.scrElement;
				var data = serverTable2.row(this).data();
				showDetailRegisters(data);
				if($(this).hasClass("selected")){
					$(this).removeClass("selected");
				}else{
					serverTable2.$('tr.selected').removeClass('selected');
					$(this).addClass("selected");
				}
				return;
			});

		}
	});
};

//========================================归档页面===================================
function archiveFinishedReport(data){
	var reportId=data.id;
	window.location.href=rootPath+"/ui/archiveReport/archiveFinishedReportUI/"+reportId;
};
function archiveFinishedReportDirectly(data){
	
	if(confirm('确定直接归档?')){
        $("#right").mLoading("show");
		$.ajax({
			url : rootPath + "/ui/archiveReport/archiveFinishedReportDirectly/" + data.id,
			dataType : 'json',
			type : 'post',
			data : data,
			success : function(result) {
				if (result.code == 1) {
					message("直接归档成功");
					window.location.href = rootPath + "/ui/archiveReport";
				}else{
					message("直接归档失败");
				}
	            $("#right").mLoading("hide");
			}
		});
	}
}
//======================已完结 尚未归档的事件源=======================================
var serverData = {};
var serverTable;
function initPage(){

	serverTable = $('#dataTable').DataTable({
		"serverSide" : true,
		"ordering" : false,
		"ajax" : {
			"dataType" : "json",
			"type" : "post",
			"cache" : false,
			"url" : window.rootPath
					+ '/ui/archiveReport/initFinishedReports',
			"data" : function(d) {
				return $.extend({}, d,
						serverData);
			}
		},
		columns : [
				{
					data : null,
					defaultContent : ""
				},
				{
					data : '',
					defaultContent : "",
					render : function(data, type, full, meta) {
						// message(data);
						var operate = '<button type="button" class="btn btn-sm btn-info archiveBtn" title="填写归档报告"><i class="fa fa-sign-in"></i></button>';
						 operate += '<button type="button" class="btn btn-sm btn-info archiveBtn2" title="直接归档"><i class="fa fa-mail-reply"></i></button>';
						return operate;
					}
				}, {
					data : 'reportContent'
				},{
//					data:'reportEventType.eventName'
					data:'reportEventTypeName'
				},{
					data:'reportAccTime',
					render:function(data,type,full,meta){
						if(data){
							return moment(data).format('YYYY/MM/DD HH:mm:ss');
						}else{
							return '';
						}
						
					}
				}, {
//					data : 'reportUser.name'
					data : 'reportUserName'
				}, {
					data : 'reportMemo',
				},{
					data:'reportAdress'
				}
		],
		"drawCallback" : function(oSettings) {
			var k = 0;
			var total = oSettings.aiDisplay.length
					+ oSettings._iDisplayStart;
			for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
				$('td:eq(0)',oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
				k++;
			}
			serverTable.off('click', 'tr');
			serverTable.on('click','tr',function(event) {
						event = event || window.event;
						var target = event.target|| event.scrElement;
						var data = serverTable.row(this).data();

						if (target.className.indexOf('fa-sign-in') != -1|| target.className.indexOf('archiveBtn') != -1) {
							archiveFinishedReport(data);
						}
						if (target.className.indexOf('fa-mail-reply') != -1|| target.className.indexOf('archiveBtn2') != -1) {
							archiveFinishedReportDirectly(data);
						}
						
						return;
					});

		}
	});
};